package com.deli.core.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.deli.core.pojo.Color;
import com.deli.core.pojo.Sku;
import com.deli.core.pojo.SuperPojo;
import com.deli.core.service.ColorService;
import com.deli.core.service.SkuService;

/**
 * 商品的库存控制器
 * @author Administrator
 *
 */
@Controller
public class SkuAction {

	@Autowired
	private SkuService skuService;
	@Autowired
	private ColorService colorService;
	// 库存
		@RequestMapping(value = "console/sku/{pageName}.do")
		public String consoleSkuShow(
				@PathVariable(value = "pageName") String pageName) {
			return "sku/" + pageName;
		}
	@RequestMapping(value="console/sku/list.do")
	public String consolrSkuShowList(Model model,Long productId){
		List<Sku> skus = skuService.findSkuById(productId);
		List<SuperPojo> superProducts = new ArrayList<SuperPojo>();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (Sku sku : skus) {
			Map<String, Object> map = new HashMap<String,Object>();
			Long colorId = sku.getColorId();
			Color color = colorService.findColorById(colorId);
			String colorName = color.getName();
			map.put("id", sku.getId());
			map.put("parendId", sku.getProductId());
			map.put("deliveFee", sku.getDeliveFee());
			map.put("marketPrice", sku.getMarketPrice());
			map.put("stock", sku.getStock());
			map.put("price", sku.getPrice());
			map.put("size", sku.getSize());
			map.put("upperLimit", sku.getUpperLimit());
			map.put("colorName",colorName);
			list.add(map);
			
		}
		
		model.addAttribute("skus", list);
		return "sku/list";
	}
	@RequestMapping(value="console/sku/update.do")
	@ResponseBody
	public Map<String,Object> consolrSkuUpdate(Model model,Sku sku){
		int i = skuService.updateById(sku);
		Map<String, Object> hashMap = new HashMap<String,Object>();
		hashMap.put("name", "修改成功");
		return hashMap;
	}
	
}
